package me.flyray.bsin.server.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.constants.ResponseCode;
import me.flyray.bsin.context.BsinServiceContext;
import me.flyray.bsin.domain.entity.ConversationMessage;
import me.flyray.bsin.exception.BusinessException;
import me.flyray.bsin.facade.service.ConversationMessageService;
import me.flyray.bsin.infrastructure.mapper.ConversationMessageMapper;
import me.flyray.bsin.mybatis.utils.Pagination;
import me.flyray.bsin.security.contex.LoginInfoContextHelper;
import me.flyray.bsin.security.domain.LoginUser;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.client.apache.dubbo.annotation.ShenyuDubboService;
import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
import org.apache.shenyu.client.apidocs.annotations.ApiModule;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

import static me.flyray.bsin.constants.ResponseCode.ID_NOT_ISNULL;

@Slf4j
@ShenyuDubboService(path = "/conversationDetail", timeout = 6000)
@ApiModule(value = "conversationDetail")
public class ConversationMessageServiceImpl implements ConversationMessageService {

    @Autowired
    private ConversationMessageMapper conversationMemoryMapper;

    @ApiDoc(desc = "delete")
    @ShenyuDubboClient("/delete")
    @Override
    public void delete(Map<String, Object> requestMap) {
        String serialNo = MapUtils.getString(requestMap, "serialNo");
        if (conversationMemoryMapper.deleteById(serialNo) == 0){
            throw new BusinessException(ID_NOT_ISNULL);
        }
    }

    @ApiDoc(desc = "getDetail")
    @ShenyuDubboClient("/getDetail")
    @Override
    public ConversationMessage getDetail(Map<String, Object> requestMap) {
        String serialNo = MapUtils.getString(requestMap, "serialNo");
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        String bizRoleTypeNo = null;
        if (serialNo == null) {
            bizRoleTypeNo = loginUser.getBizRoleTypeNo();
            if(bizRoleTypeNo == null){
                throw new BusinessException(ResponseCode.CUSTOMER_NO_NOT_ISNULL);
            }
        }
        ConversationMessage conversationMemory = conversationMemoryMapper.selectOne(new LambdaQueryWrapper<ConversationMessage>()
                .eq(ConversationMessage::getTenantId, loginUser.getTenantId())
                .eq(StringUtils.isNotBlank(serialNo), ConversationMessage::getSerialNo, serialNo)
                .eq(StringUtils.isNotBlank(bizRoleTypeNo), ConversationMessage::getBizRoleTypeNo, bizRoleTypeNo));
        return conversationMemory;
    }

    @ApiDoc(desc = "getPageList")
    @ShenyuDubboClient("/getPageList")
    @Override
    public IPage<?> getPageList(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        ConversationMessage conversationMemory = BsinServiceContext.getReqBodyDto(ConversationMessage.class, requestMap);
        Object paginationObj =  requestMap.get("pagination");
        Pagination pagination = new Pagination();
        BeanUtil.copyProperties(paginationObj,pagination);

        Page<ConversationMessage> page = new Page<>(pagination.getPageNum(),pagination.getPageSize());
        LambdaQueryWrapper<ConversationMessage> warapper = new LambdaQueryWrapper<>();
        warapper.orderByDesc(ConversationMessage::getCreateTime);
        warapper.eq(ConversationMessage::getTenantId, loginUser.getTenantId());
        warapper.eq(StringUtils.isNotBlank(loginUser.getMerchantNo()), ConversationMessage::getBizRoleTypeNo, loginUser.getBizRoleTypeNo());
        IPage<ConversationMessage> pageList = conversationMemoryMapper.selectPage(page,warapper);
        return pageList;
    }

    @ApiDoc(desc = "getList")
    @ShenyuDubboClient("/getList")
    @Override
    public List<ConversationMessage> getList(Map<String, Object> requestMap) {
        ConversationMessage conversationMemory = BsinServiceContext.getReqBodyDto(ConversationMessage.class, requestMap);
        String tenantId = (String) requestMap.get("tenantId");
        LambdaQueryWrapper<ConversationMessage> warapper = new LambdaQueryWrapper<>();
        warapper.eq(ConversationMessage::getTenantId, tenantId);
        List<ConversationMessage> agentCardList = conversationMemoryMapper.selectList(warapper);
        return agentCardList;
    }

}
